package com.itheima.web.servlet;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;

//@WebServlet("/updateServlet")
public class UpdateServlet extends HttpServlet {
    private BrandService brandService = new BrandServiceImpl();


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.接收修改后的品牌数据
        BufferedReader br = request.getReader();
        String params = br.readLine();

        //将接收的id转为int型
        int id = Integer.parseInt(request.getParameter("id"));

        //转为Brand对象
        Brand _brand = JSON.parseObject(params, Brand.class);

        //将需要修改的数据封装成brand对象
        Brand brand = new Brand();
        brand.setId(id);
        brand.setBrandName(_brand.getBrandName());
        brand.setCompanyName(_brand.getCompanyName());
        brand.setOrdered(_brand.getOrdered());
        brand.setDescription(_brand.getDescription());
        brand.setStatus(_brand.getStatus());


        //2.调用service修改
        brandService.update(brand);

        //3.响应成功的标识
        response.getWriter().write("updateSuccess");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
